﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using AspNetCs.Model;
using AspNetCs.IDAL;
using AspNetCs.DALFactory;
using AspNetCs.SQLServerDAL;
using System.Collections;
using AspNetCs.Config;
using System.Web;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;

namespace AspNetCs.Bll
{
    public class AncestralHallBLL
    {
        private IAncestralHall dal = Conn.CreateAncestralHall();

        public DataTable GetDataTable(string filter)
        {
            return dal.GetDataTable(filter);
        }

        /// <summary>
        /// 是否存在祠堂编号
        /// </summary>
        /// <param name="AncestralHallId">祠堂编号</param>
        /// <returns></returns>
        public bool IsExistAncestralHallId(string AncestralHallId)
        {
            return dal.IsExistAncestralHallId(AncestralHallId);
        }

        /// <summary>
        /// 生成宗祠表中不存在的随机数
        /// </summary>
        /// <param name="list">不允许出现的随机数</param>
        /// <returns></returns>
        public string RndNum(ArrayList list, int Rndlen)
        {
            return dal.RndNum(list, Rndlen);
        }

        /// <summary>
        /// 添加宗祠
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(AncestralHall_Model model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 修改宗祠
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(AncestralHall_Model model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除宗祠(物理删除)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            return dal.Delete(id);
        }

        /// <summary>
        /// 删除宗祠(逻辑删除)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Hidden(int id)
        {
            return dal.Hidden(id);
        }

        /// <summary>
        /// 获得创建的宗祠
        /// </summary>
        /// <param name="CreateId">创建者编号</param>
        /// <param name="RequeryItems">查询的字段</param>
        /// <param name="IsDel">是否查询的被删除的信息 0:没有删除的 1:删除了的 2:所有的</param>
        /// <returns></returns>
        public DataTable getMyCreateAncestralHallItems(int CreateId, string RequeryItems, int IsDel)
        {
            DataTable dataTable = dal.getMyCreateAncestralHallItems(CreateId, RequeryItems, 0);

            return dataTable;
        }

        /// <summary>
        /// 通过编号获得宗祠详细信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public DataTable GetAncestralHallInfoById(int Id)
        {
            return dal.GetAncestralHallInfoById(Id);
        }


        /// <summary>
        /// 设置推荐或者取消推荐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示设置推荐 0表示取消推荐</param>
        /// <returns></returns>
        public int SetIntro(int Id, int CancleOrIntro)
        {
            return dal.SetIntro(Id, CancleOrIntro);
        }

        /// <summary>
        /// 审核或者取消审核
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示审核 0表示取消审核</param>
        /// <returns></returns>
        public int SetCheck(int Id, int CancleOrIntro)
        {
            return dal.SetCheck(Id, CancleOrIntro);

        }


        /// <summary>
        /// 设置背景音乐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="MusicId"></param>
        /// <returns></returns>
        public int SetMusic(int Id, int MusicId)
        {
            return dal.SetMusic(Id, MusicId);

        }

        public AncestralHall_Model GetModel(int id)
        {
            AncestralHall_Model md = new AncestralHall_Model();
            DataTable dt = GetDataTable(" id=" + id);
            foreach (DataRow dr in dt.Rows)
            {
                md = GetModel(dr);
            }
            return md;
        }

        public AncestralHall_Model GetModel(DataRow dr)
        {
            AncestralHall_Model Mdl = dal.GetModel(dr);
            return Mdl;
        }


        /// <summary>
        /// 根据当前馆号,生成用模板
        /// </summary>
        /// <param name="NumStr">馆号</param>
        /// <param name="chel"></param>
        /// <param name="systemConfig">系统配制信息</param>
        /// <param name="htmlpath">生成的路径</param>
        /// <returns></returns>
        public bool CurrTempleContent(string NumStr, Channel_Type chel, SiteConfigInfo systemConfig, string htmlpath)
        {
            bool Result = false;
            StringBuilder stb = new StringBuilder();
            AncestralHall_Model md = new AncestralHall_Model();
            try
            {

                DataTable dt = GetDataTable(" AncestralHallId='" + NumStr + "'");
                dt.TableName = "AncestralHall";
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        md = GetModel(dr);
                        break;
                    }
                    if (md.TempleId > 0)
                    {
                        Temple_Model TmpMd = TempleBll.CreateNew.GetModel(md.TempleId);
                        if (TmpMd.Id > 0)
                        {
                            string TempleUrl = systemConfig.InstallDir + StringHelper.ReadConfig("TempleSkin") + chel.ToString() + "/" + TmpMd.TemplePath + "/index.html";
                            string Templepath = HttpContext.Current.Server.MapPath(TempleUrl);//模板的真实路径

                            StringBuilder newstr = new StringBuilder();
                            newstr.AppendLine("<script>");
                            newstr.AppendLine("<!--");
                            newstr.AppendLine("var Numid='" + md.AncestralHallId + "';");//馆号
                            newstr.AppendLine("var id='" + md.Id + "';");//ID
                            newstr.AppendLine("var UserID='" + md.CreateId + "';");//名称
                            newstr.AppendLine("var Pid='" + md.Province + "';");//省份
                            newstr.AppendLine("var CityID='" + md.City + "';");//城市
                            newstr.AppendLine("-->");
                            newstr.AppendLine("</script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/jquery.js'></script>");


                            stb = TempleBll.CreateNew.WriteHtml(Templepath, newstr.ToString());//读取模板内容




                            #region 宗祠的静态标签替换
                            DataTable DtItems = AncestralHallItems_BLL.CreateNew.getAncestralHallItemsByAncestralHallId(md.Id, "*", 0);
                            try
                            {
                                DataRow[] dr1 = DtItems.Select(" ItemName='" + md.PreName + "姓族谱'");
                                if (dr1.Length > 0)
                                {
                                    stb = stb.Replace("[$?姓族谱$]", "<a href='" + System_Lable.SystemGetCache.InstallDir + "AncestralHallBBS/BBSIndex.Aspx?AncestralHallId=" + md.Id.ToString() + "&AncestralHallItemId=" + dr1[0]["id"].ToString() + "&accessType=1'>" + md.PreName + "姓族谱</a>");
                                }
                                else
                                {
                                    stb = stb.Replace("[$?姓族谱$]", md.PreName + "姓族谱");
                                }
                            }
                            catch
                            {

                            }
                            try
                            {
                                DataRow[] dr2 = DtItems.Select(" ItemName='宗祠规划'");

                                if (dr2.Length > 0)
                                {
                                    stb = stb.Replace("[$宗祠规划$]", "<a href='" + System_Lable.SystemGetCache.InstallDir + "AncestralHallBBS/BBSIndex.aspx?AncestralHallId=" + md.Id.ToString() + "&AncestralHallItemId=" + dr2[0]["id"].ToString() + "&accessType=1'>宗祠规划</a>");
                                }
                                else
                                {
                                    stb = stb.Replace("[$宗祠规划$]", "宗祠规划");
                                }
                            }
                            catch
                            {

                            }

                            try
                            {
                                DataRow[] dr3 = DtItems.Select(" ItemName='宗祠留言'");

                                if (dr3.Length > 0)
                                {
                                    stb = stb.Replace("[$本氏留言$]", "<a href='" + System_Lable.SystemGetCache.InstallDir + "AncestralHallBBS/BBSIndex.aspx?AncestralHallId=" + md.Id.ToString() + "&AncestralHallItemId=" + dr3[0]["id"].ToString() + "&accessType=1'>本氏留言</a>");
                                }
                                else
                                {
                                    stb = stb.Replace("[$本氏留言$]", "本氏留言");
                                }
                            }
                            catch
                            {

                            }

                            stb = stb.Replace("[$?氏论坛$]", "<a href='" + System_Lable.SystemGetCache.InstallDir + "AncestralHallBBS/BBSIndex.Aspx?AncestralHallId=" + md.Id.ToString() + "&accessType=0'>" + md.PreName + "氏论坛</a>");

                            stb = stb.Replace("[$宗祠介绍$]", "<a href='" + System_Lable.SystemGetCache.InstallDir + "ClanShow.aspx?id=" + md.Id + "'>宗祠介绍</a>");


                            stb = stb.Replace("[$祭奠浏览$]", "<a href='" + System_Lable.SystemGetCache.InstallDir + "clan_Sacrifice-" + md.Id + ".Aspx'>祭奠浏览</a>");

                            stb = stb.Replace("[$本宗建馆$]", "<a href='" + System_Lable.SystemGetCache.InstallDir + "Users/Create_memorial.Aspx'>本宗建馆</a>");

                            stb = stb.Replace("[$宗祠功德$]", "<a href='" + System_Lable.SystemGetCache.InstallDir + "clanAlms-" + md.Id + ".Aspx'>宗祠功德</a>");




                            #endregion


                            stb = Script_Lable.ScriptLable_Replace(stb, chel, md.Id.ToString());
                            GetStringData_lable lbReplace = new GetStringData_lable();
                            string ContentValue = lbReplace.GetResultStr(stb, dt);
                            Result = TempleBll.CreateNew.CreateHtmlFile(ContentValue, htmlpath);
                        }
                    }
                }
            }
            catch
            {
                Result = false;
            }

            return Result;
        }

        /// <summary>
        /// 更新字段
        /// </summary>
        /// <param name="Upfilter">更新字段</param>
        /// <param name="filter">条件</param>
        public void filterUpdate(string Upfilter, string filter)
        {
            if (Upfilter != "")
            {
                dal.filterUpdate(Upfilter, filter);
            }
        }

    }
}
